//
//  AllotOrderWhsOutViewController.m
//  TryShopping
//
//  Created by panquanReabam.com on 16/9/19.
//  Copyright © 2016年 RuiBen. All rights reserved.
//
#import "AllotOrderWhsOutViewController.h"
#import "UIViewExt.h"
#import "HttpRequest.h"
#import "JSONKit.h"
#import "UIImageView+WebCache.h"
#import "RDVTabBarController.h"
#import "MJRefresh.h"
#import "ChoosePaymentViewController.h"

#import "huiyuanDetailsCell.h"
@interface AllotOrderWhsOutViewController ()<MBProgressHUDDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate,UITextFieldDelegate,UITextViewDelegate,UISearchBarDelegate,UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>
@end
@implementation AllotOrderWhsOutViewController
{
    UITextView* tv;
    UILabel * label;
    UIImageView*imagess;
    UIView* myToolBar;
    NSMutableArray*quantity;
    NSMutableArray *itemCodeArray;
    NSMutableArray *itemIdArray;
    NSMutableArray *selectimageArray;
    NSMutableArray *outQuantityArray;
    NSMutableArray *inQuantityArray;
    NSMutableArray *noChangeOutQuantityArray;
    NSMutableArray *dealPriceArray;
    NSMutableArray *selectitemCodeArray;
    NSMutableArray *specNameArray;
    NSMutableArray *itemNameArray;
    NSString*str;
    NSMutableArray *specIdArray;
    UITableView*table;
    NSMutableArray* itemsArray;
    UIButton*hejibuyBtn1;
    double sum1;
    UIButton*totalQuantity;
    UIButton* quantityBtn;
    NSString*shuliang;
    NSString*tagStr;
}
@synthesize alt = alt;

-(void)BackAction:(UIButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];
    
}
- (void)viewDidDisappear:(BOOL)animated
{
    
    [super viewDidDisappear:animated];
    self.navigationController.delegate = nil;
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [[self rdv_tabBarController] setTabBarHidden:YES];
    self.navigationController.navigationBarHidden=NO;
    self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.textAlignment=NSTextAlignmentLeft;
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    self.navigationController.delegate = self;
    
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
}
- (void)viewDidLoad
{
    
    [super viewDidLoad];
    self.title=self.titleName;
    itemCodeArray=[[NSMutableArray alloc] init];
    selectimageArray=[[NSMutableArray alloc] init];
    outQuantityArray=[[NSMutableArray alloc] init];
    noChangeOutQuantityArray=[[NSMutableArray alloc] init];
    dealPriceArray=[[NSMutableArray alloc] init];
    itemNameArray=[[NSMutableArray alloc] init];
    itemIdArray=[[NSMutableArray alloc] init];
    specNameArray=[[NSMutableArray alloc] init];
    quantity= [[NSMutableArray alloc] init];
    specIdArray= [[NSMutableArray alloc] init];
    inQuantityArray= [[NSMutableArray alloc] init];
    table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStylePlain];
    table.delegate = self;
    table.dataSource = self;
    table.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:table];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [UIColor blackColor],
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];
    
    
    UIView* view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 385/2)];
    view.backgroundColor=zuiqianhuiColor;
    UIView*view1=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 40)];
    view1.backgroundColor=[UIColor whiteColor];
    [view addSubview:view1];
    UILabel *line1=[[UILabel alloc] initWithFrame:CGRectMake(0, 40, ScreenWidth, 0.40)];
    line1.backgroundColor=qianhuiColor;
    [view addSubview:line1];
    UILabel* userName=[[UILabel alloc]initWithFrame:CGRectMake(15, 0, 40, 40)];
    userName.text=[NSString stringWithFormat:@"员工"];
    userName.textColor=shenhuiColor;
    userName.font=[UIFont systemFontOfSize:15];
    [view addSubview:userName];
    
    NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
    NSString* UserName = [user1 objectForKey:@"userName"];
    UIButton*userNameBtn=[[UIButton alloc]initWithFrame:CGRectMake(55, 0,ScreenWidth-100,40)];
    [userNameBtn setTitle:UserName forState:UIControlStateNormal];
    userNameBtn.titleLabel.font=[UIFont systemFontOfSize:15];
    userNameBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [userNameBtn setTitleColor:qianheiColor forState:UIControlStateNormal];
    [view addSubview:userNameBtn];
    UIButton*zhankai = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth-40,5,33, 33)];
    [zhankai setImage:[UIImage imageNamed:@"zhankai"] forState:UIControlStateNormal];
    [view addSubview:zhankai];
    UIView*view2=[[UIView alloc]initWithFrame:CGRectMake(0, view1.bottom+10, ScreenWidth, 45*2)];
    view2.backgroundColor=[UIColor whiteColor];
    view2.layer.borderWidth = 0.40;
    view2.layer.borderColor = qianhuiColor.CGColor;
    [view addSubview:view2];
        
    UILabel *line3=[[UILabel alloc] initWithFrame:CGRectMake(10,  45, ScreenWidth-20, 0.40)];
    line3.backgroundColor=qianhuiColor;
    [view2 addSubview:line3];
        
    UILabel*typeLabel=[[UILabel alloc]initWithFrame:CGRectMake(15,0, 200,45)];
    typeLabel.text=@"类型";
    typeLabel.textColor=shenhuiColor;
    typeLabel.font=[UIFont systemFontOfSize:contenfot];
    [view2 addSubview:typeLabel];
    UILabel*typeLabel1=[[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth-200,0, 185,45)];
    typeLabel1.text=[[self.Dic objectForKey:@"orderData"]objectForKey:@"typeName"];
    typeLabel1.textColor=shenhuiColor;
    typeLabel1.textAlignment=NSTextAlignmentRight;
    typeLabel1.font=[UIFont systemFontOfSize:contenfot];
    [view2 addSubview:typeLabel1];

    
    UILabel*whsLabel=[[UILabel alloc]initWithFrame:CGRectMake(15,45, 200, 45)];
    whsLabel.text=@"仓库";
    whsLabel.textColor=shenhuiColor;
    whsLabel.font=[UIFont systemFontOfSize:contenfot];
    [view2 addSubview:whsLabel];
    UILabel*whsLabel1=[[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth-200,45, 185,45)];
    if ([self.titleName isEqualToString:@"确认入库"]) {
          whsLabel1.text=[[self.Dic objectForKey:@"orderData"]objectForKey:@"inWhsName"];
    }else{
      whsLabel1.text=[[self.Dic objectForKey:@"orderData"]objectForKey:@"outWhsName"];
    }
  
    whsLabel1.textColor=shenhuiColor;
    whsLabel1.textAlignment=NSTextAlignmentRight;
    whsLabel1.font=[UIFont systemFontOfSize:contenfot];
    [view2 addSubview:whsLabel1];
   
    UIView*view3=[[UIView alloc]initWithFrame:CGRectMake(0, view2.bottom+10, ScreenWidth, 45)];
    view3.backgroundColor=[UIColor whiteColor];
    [view addSubview:view3];
    
    
    UILabel*shangpingxinxi=[[UILabel alloc]initWithFrame:CGRectMake(15, 0, 80, 45)];
    shangpingxinxi.text=@"商品信息";
    shangpingxinxi.textColor=qianheiColor;
    shangpingxinxi.font=[UIFont systemFontOfSize:15];
    [view3 addSubview:shangpingxinxi];
    UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0.40)];
    lineLabel1.backgroundColor=qianhuiColor;
    [view3 addSubview:lineLabel1];
    UILabel *lineLabel2=[[UILabel alloc] initWithFrame:CGRectMake(10, 45-0.40, ScreenWidth-20, 0.40)];
    lineLabel2.backgroundColor=qianhuiColor;
    [view3 addSubview:lineLabel2];

        table.tableHeaderView=view;
        itemsArray=[[self.Dic objectForKey:@"orderData"]objectForKey:@"items"];
        if (itemsArray.count>0) {
                                for (NSDictionary* dicc in itemsArray )
                    {
                        if ([self.titleName isEqualToString:@"确认入库"]&&[[NSString stringWithFormat:@"%@",[dicc valueForKey:@"outQuantity"]]isEqualToString:@"0"]
                            ) {
                            
                        }else{
                        
                        [dealPriceArray addObject:[dicc valueForKey:@"dealPrice"]];
                        [selectimageArray addObject:[dicc valueForKey:@"imageUrlFull"]];
                        [itemIdArray addObject:[dicc valueForKey:@"itemId"]];
                        [itemNameArray addObject:[dicc valueForKey:@"itemName"]];
//                        [inQuantityArray addObject:[dicc valueForKey:@"inQuantity"]];
                            if ([self.titleName isEqualToString:@"确认入库"]) {
                                [outQuantityArray addObject:[dicc valueForKey:@"outQuantity"]];
                                [noChangeOutQuantityArray addObject:[dicc valueForKey:@"outQuantity"]];
                            }else{
                                [outQuantityArray addObject:[dicc valueForKey:@"quantity"]];
                                [noChangeOutQuantityArray addObject:[dicc valueForKey:@"quantity"]];
                       }
                        [quantity addObject:[dicc valueForKey:@"quantity"]];
                        [itemCodeArray addObject:[dicc valueForKey:@"itemCode"]];
                        [specNameArray addObject:[dicc valueForKey:@"specName"]];
                        [specIdArray addObject:[dicc valueForKey:@"specId"]];
                        
                        }
                    }
                    
                }
                
    [table reloadData];
    UIView*footview=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth,300)];
    footview.backgroundColor=zuiqianhuiColor;
    
    UIView*footerview=[[UIView alloc]initWithFrame:CGRectMake(0, 10, ScreenWidth, 110)];
    footerview.backgroundColor=[UIColor whiteColor];
    footerview.layer.borderWidth = 0.40;
    footerview.layer.borderColor = qianhuiColor.CGColor;
    [footview addSubview:footerview];
    tv=[[UITextView alloc]init];
    tv.frame=CGRectMake(15, 10, ScreenWidth-30, 90);
    tv.backgroundColor = [UIColor whiteColor];//设置它的背景颜色/Users/jiezsoft/Desktop/
    tv.scrollEnabled = YES;//是否可以拖动
    tv.delegate=self;
    //                tv.autoresizingMask = UIViewAutoresizingFlexibleHeight;//自适应高
    tv.font = [UIFont fontWithName:@"Arial" size:15.0];//设置字体名字和字体大小
    tv.layer.borderWidth =0;
    CGColorSpaceRef registedSpace11 = CGColorSpaceCreateDeviceRGB();
    CGColorRef tureBTncolorref111 = CGColorCreate(registedSpace11,(CGFloat[]){ 0, 0, 0, 0.3 });
    //                tv.keyboardType = UIKeyboardTypePhonePad;
    [tv.layer setBorderColor:tureBTncolorref111];//边框颜色
    tv.layer.cornerRadius =0;
    [footerview addSubview:tv];
    imagess=[[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 18, 16)];
    imagess.image=[UIImage imageNamed:@"_0000_xingxifankui.png"];
    [tv addSubview:imagess];
    label=[[UILabel alloc]init];
    label.frame =CGRectMake(28, 5, ScreenWidth-60, 15);
    label.text = @"备注";
    label.textColor=qianhuiColor;
    label.numberOfLines=0;
    label.font=[UIFont systemFontOfSize:15];
    label.backgroundColor = [UIColor clearColor];
    [tv addSubview:label];
    
    myToolBar=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, viewheight)];
    myToolBar.backgroundColor=jianpancolor;
    UIButton*  tubtnDone=[[UIButton alloc]initWithFrame:CGRectMake(logoX,logoY, logoheight, logowidth)];
    [tubtnDone setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [tubtnDone addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
    [myToolBar addSubview:tubtnDone];
    UIButton*btnDone111=[[UIButton alloc]initWithFrame:CGRectMake(myToolBar.frame.size.width - 55, 12, 40, 16)];
    [btnDone111 setTitle:@"确定" forState:UIControlStateNormal];
    [btnDone111 setTitleColor:jianpancolor1 forState:UIControlStateNormal];
    btnDone111 .titleLabel .font=[UIFont systemFontOfSize:keyboardFot];
    [btnDone111 addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
    [myToolBar addSubview:btnDone111];
    myToolBar.alpha=alp;
    UIButton* dabtn=[[UIButton alloc]initWithFrame:CGRectMake(myToolBar.frame.size.width-60, 0, 60, 40)];
    [dabtn addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
    [myToolBar addSubview:dabtn];

    
    tv.inputAccessoryView = myToolBar;
    table.tableFooterView= footview;

    [self heji];
    
}
-(void)heji{
    
    NSMutableArray*  sum=[[NSMutableArray alloc]init];
    for (int i=0; i<outQuantityArray.count; i++) {
        NSString *typeCode1 = [outQuantityArray objectAtIndex:i];
        NSString *isSingle1 = [dealPriceArray objectAtIndex:i];
        [sum addObject:[NSString stringWithFormat:@"%.2f",  [typeCode1 doubleValue]*[isSingle1 doubleValue]]];
    }
    
    NSNumber *sum111 = [sum valueForKeyPath:@"@sum.floatValue"];
    NSString*GoldPrice=[NSString stringWithFormat:@"%@",sum111];
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setPositiveFormat:@"###,##0.00;"];
    NSString *formattedNumberString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:GoldPrice.floatValue]];
   
    
    totalQuantity=[[UIButton alloc]initWithFrame:CGRectMake(0, ScreenHeight-48, ScreenWidth*430/750, 45)];
    [totalQuantity setTitle:[NSString stringWithFormat:@"   合计 ¥ %@",formattedNumberString] forState:UIControlStateNormal];
    totalQuantity.backgroundColor=[UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1.0];
    [totalQuantity setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    totalQuantity.alpha=0.8;
    totalQuantity.contentHorizontalAlignment=UIControlContentHorizontalAlignmentFill;
    totalQuantity.contentVerticalAlignment=UIControlContentVerticalAlignmentBottom;
    totalQuantity.titleLabel.font=[UIFont systemFontOfSize:18];
    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"   合计 ¥ %@",formattedNumberString]];
    
    [AttributedStr addAttribute:NSFontAttributeName
     
                          value:[UIFont systemFontOfSize:13]
     
                          range:NSMakeRange(0, 7)];
    
    [AttributedStr addAttribute:NSForegroundColorAttributeName
     
                          value:[UIColor whiteColor]
     
                          range:NSMakeRange(0, 7)];
    
    
    totalQuantity.titleLabel.attributedText = AttributedStr;
    
    quantityBtn=[[UIButton alloc]initWithFrame:CGRectMake(0, 9, ScreenWidth*430/750, 11)];
    NSNumber *sum11 = [outQuantityArray valueForKeyPath:@"@sum.floatValue"];
    [quantityBtn setTitle:[NSString stringWithFormat:@"   共 %@ 件商品",sum11] forState:UIControlStateNormal];
    quantityBtn.backgroundColor=[UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1.0];
    [quantityBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    quantityBtn.titleLabel.font=[UIFont systemFontOfSize:11];
    quantityBtn.alpha=0.8;
    quantityBtn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
    [totalQuantity addSubview:quantityBtn];
    [self.view addSubview:totalQuantity];
    
    UIButton*dise=[[UIButton alloc]initWithFrame:CGRectMake(0, ScreenHeight-3, ScreenWidth*430/750, 3)];
    dise.backgroundColor=[UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1.0];
    dise.alpha=0.8;
    [self.view addSubview:dise];
    
    UIButton*buyBtn=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth*430/750, ScreenHeight-48, ScreenWidth*320/750, 48)];
    [buyBtn setTitle:@"确定" forState:UIControlStateNormal];
    buyBtn.backgroundColor=luseColor;
    [buyBtn addTarget:self action:@selector(ture:) forControlEvents:UIControlEventTouchUpInside];
    [buyBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:buyBtn];
    
}
- (void)ture:(UIButton *)button{
    
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        NSString* Token = [user objectForKey:@"tokenId"];
        NSMutableDictionary *params = [NSMutableDictionary new];
        [params setValue:Token forKey:@"tokenId"];
        [params setValue:self.orderId forKey:@"orderId"];
    
        [params setValue:tv.text forKey:@"remark"];
    NSMutableArray *mDocumentLine=[NSMutableArray array];
    for (int i=0; i<itemIdArray.count; i++) {
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        NSString *itemId = [itemIdArray objectAtIndex:i];
        NSString *quantity1 =[outQuantityArray objectAtIndex:i];
    
        NSString *specId =[NSString stringWithFormat:@"%@",[specIdArray objectAtIndex:i]];
        [dic setValue:quantity1 forKey:@"quantity"];

        [dic setValue:itemId forKey:@"itemId"];
        if ([[NSString stringWithFormat:@"%@",specId ] isEqualToString:@""]) {
            specId=@"";
        }else{
            if ([[NSString stringWithFormat:@"%@",specId ] isEqualToString:@"<null>"]) {
                specId=@"";
            }
            [dic setValue:specId forKey:@"specId"];
        }
        
        [mDocumentLine addObject:dic];
    }
    if ([self.titleName isEqualToString:@"确认入库"]) {
       str =@"Allot/Order/WhsIn";
    }else{
       str =@"Allot/Order/WhsOut";
    }
    
      [params setValue:mDocumentLine  forKey:@"items"];
        [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:20 view:self.view];
       NSLog(@"33333333====%@",params);
        [HttpRequest requestData:str params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
            if (isSuccess) {
               NSLog(@"33333333====%@",severDataDict.dataDict);
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                    [Global hideProgressHUDInternal:self.view];
                    [_mDelegate update:@""];
                   [self.navigationController popViewControllerAnimated:YES];
                }else{
                    
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    [self showAlertMessage:requestResultStr];
                  [Global hideProgressHUDInternal:self.view];
//                  
//                    [Global showTextHudSub:requestResultStr view:self.view];
                }
                
            }
            else
            {
                [Global hideProgressHUDInternal:self.view];
                [Global showTextHudSub:severDataDict.resultStr view:self.view];
            }
            
        }];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [dealPriceArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    huiyuanDetailsCell* cell = (huiyuanDetailsCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"huiyuanDetailsCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    UIButton*jiantou = [[UIButton alloc]init];
    jiantou.frame = CGRectMake(0,0, 8, 13);
    [jiantou setImage:[UIImage imageNamed:@"xiangzuo-.png"] forState:UIControlStateNormal];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
 
    [cell.imagebtn.layer setCornerRadius:6.0];
    [cell.imagebtn.layer setBorderWidth:0.3];
    [cell.imagebtn.layer setMasksToBounds:YES];
    CGColorSpaceRef colorSpace2 = CGColorSpaceCreateDeviceRGB();
    CGColorRef colorref2 = CGColorCreate(colorSpace2,(CGFloat[]){ 0.93, 0.93, 0.93, 0.93 });
    [cell.imagebtn.layer setBorderColor:colorref2];//边框颜色
     [cell.imagebtn setImageWithURL:[NSURL URLWithString:[selectimageArray objectAtIndex:indexPath.row]] placeholderImage:[UIImage imageNamed:@"zhengfangxing"]];
    cell.imagebtn.frame=CGRectMake(ScreenWidth*30/750,  20,  170/2,  170/2);
    
    
    
    cell.CreateDate.text =[NSString stringWithFormat:@"%@ %@",[itemCodeArray objectAtIndex:indexPath.row],[itemNameArray objectAtIndex:indexPath.row]];
    cell.CreateDate.font=[UIFont systemFontOfSize:14];
    cell.CreateDate.textColor=[UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1];
    cell.CreateDate.frame=CGRectMake(cell.imagebtn.right+10, 17.5, 520*ScreenWidth/750,35);
    cell.CreateDate.numberOfLines=0;
   
    cell.Memo.frame=CGRectMake(cell.imagebtn.right+10, 70, 400*ScreenWidth/750,13);
    if ([specNameArray objectAtIndex:indexPath.row] ==NULL|| [[Global getJudgeTheErrorStr:[specNameArray objectAtIndex:indexPath.row]]isEqualToString:@""]) {
            cell.Memo.text =[NSString stringWithFormat:@"%@",@" "];
        }else{
            cell.Memo.text =[NSString stringWithFormat:@"%@",[specNameArray objectAtIndex:indexPath.row]];}
    
        cell.Memo.font=[UIFont systemFontOfSize:13];
        cell.Memo.textColor=zhonghuiColor;
    
    
    UIFont *font1 = [UIFont systemFontOfSize:contenfot];
    CGSize size1 = CGSizeMake(ScreenWidth-60, 2000);
    CGSize labelsize1 = [[NSString stringWithFormat:@"数量 %.f",  [[quantity objectAtIndex:indexPath.row]floatValue]] sizeWithFont:font1 constrainedToSize:size1 lineBreakMode:NSLineBreakByCharWrapping];

    
        cell.Integral.frame=CGRectMake(cell.imagebtn.right+10, 85, labelsize1.width+5,13);
        cell.Integral.text =[NSString stringWithFormat:@"数量 %.f",  [[quantity objectAtIndex:indexPath.row]floatValue]];
        cell.Integral.font=[UIFont systemFontOfSize:13];
    NSMutableAttributedString *AttributedStr12 = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"数量 %.f",  [[quantity objectAtIndex:indexPath.row]floatValue]]];
    
    [AttributedStr12 addAttribute:NSFontAttributeName
     
                           value:[UIFont systemFontOfSize:13]
     
                           range:NSMakeRange(0, 2)];
    
    [AttributedStr12 addAttribute:NSForegroundColorAttributeName
     
                           value:zhonghuiColor
     
                           range:NSMakeRange(0, 2)];
    
    cell.Integral.attributedText = AttributedStr12;


    
    NSString*GoldPrice=[NSString stringWithFormat:@"%@",[dealPriceArray objectAtIndex:indexPath.row]];
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setPositiveFormat:@"###,##0.00;"];
    NSString *formattedNumberString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:GoldPrice.floatValue]];
    
    cell.Price.text =[NSString stringWithFormat:@"￥%@",  formattedNumberString];
    cell.Price.font=[UIFont systemFontOfSize:16];
    cell.Price.textColor=qianheiColor;
    cell.Price.frame=CGRectMake(cell.Integral.right+10, 82, 400*ScreenWidth/750,16);
    NSMutableAttributedString *AttributedStr2 = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"￥%@",  formattedNumberString]];
    
    [AttributedStr2 addAttribute:NSFontAttributeName
     
                           value:[UIFont systemFontOfSize:13]
     
                           range:NSMakeRange(0, 1)];
    
    [AttributedStr2 addAttribute:NSForegroundColorAttributeName
     
                           value:zhonghuiColor
     
                           range:NSMakeRange(0, 1)];
    
    cell.Price.attributedText = AttributedStr2;

    
    UIView*countParentView=[[UIView alloc] initWithFrame:CGRectMake(ScreenWidth-125, 100, 110, 30)];
    countParentView.layer.borderWidth=1.0;
    countParentView.layer.borderColor=[UIColor colorWithRed:240.0/255.0f green:240.0/255.0f blue:240.0/255.0f alpha:240.0/255.0f].CGColor;
    [cell.contentView addSubview:countParentView];
    
    cell.time.text=[NSString stringWithFormat:@"%@",  outQuantityArray[indexPath.row]];
    cell.time.textColor=qianheiColor;
    cell.time.center=countParentView.center;
    cell.time.textAlignment=NSTextAlignmentCenter;
    cell.time.font=[UIFont systemFontOfSize:16];
    [cell.contentView addSubview:cell.time];
    cell.how=[NSString stringWithFormat:@"%@",  [noChangeOutQuantityArray objectAtIndex:indexPath.row]];

    UIButton *CountBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [CountBtn setFrame:CGRectMake(0, 0, 50,30)];
    CountBtn.tag=indexPath.row;
    CountBtn.center=countParentView.center;
    [CountBtn addTarget:self action:@selector(countBtn:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:CountBtn];
    UIButton*jian=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth-125, 100, 30, 30)];
    jian.tag=indexPath.row;
    [jian setImage:[UIImage imageNamed:@"jian_hui.png"] forState:UIControlStateNormal];
    [jian addTarget:self action:@selector(jian:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:jian];
    
    UIButton*jia=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth-45,100, 30, 30)];
    jia.tag=indexPath.row;
    [jia setImage:[UIImage imageNamed:@"jia.png"] forState:UIControlStateNormal];
    [jia addTarget:self action:@selector(jia:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:jia];

//    UIImageView*shuimageview=[[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth-125, 100, 110, 31)];
//    shuimageview .image=[UIImage imageNamed:@"tianjias"];
//    [cell.contentView addSubview:shuimageview];
//    
//    cell.time.text=[NSString stringWithFormat:@"%@",  [outQuantityArray objectAtIndex:indexPath.row]];
//    cell.time.textColor=qianheiColor;
//    cell.time.center=shuimageview.center;
//    cell.time.textAlignment=NSTextAlignmentCenter;
//    cell.time.font=[UIFont systemFontOfSize:16];
//    [cell.contentView addSubview:cell.time];
//    
//    cell.how=[NSString stringWithFormat:@"%@",  [noChangeOutQuantityArray objectAtIndex:indexPath.row]];
//    
//    UIButton *CountBtn=[UIButton buttonWithType:UIButtonTypeCustom];
//    [CountBtn setFrame:CGRectMake(0, 0, 45,34)];
//    CountBtn.tag=indexPath.row;
//    CountBtn.center=shuimageview.center;
//    [CountBtn addTarget:self action:@selector(countBtn:) forControlEvents:UIControlEventTouchUpInside];
//    [cell.contentView addSubview:CountBtn];
//    UIButton*jian=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth-125, 100, 33, 31)];
//    jian.tag=indexPath.row;
//    [jian addTarget:self action:@selector(jian:) forControlEvents:UIControlEventTouchUpInside];
//    [cell.contentView addSubview:jian];
//    
//    UIButton*jia=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth-45,100, 33, 31)];
//    jia.tag=indexPath.row;
//    [jia setImage:[UIImage imageNamed:@"jia.png"] forState:UIControlStateNormal];
//    [jia addTarget:self action:@selector(jia:) forControlEvents:UIControlEventTouchUpInside];
//    [cell.contentView addSubview:jia];
//    
    
    if ([self.titleName isEqualToString:@"确认入库"]) {
        cell.Amount.frame=CGRectMake(cell.imagebtn.right+10, 112, 400*ScreenWidth/750,13);
        cell.Amount.text=[NSString stringWithFormat:@"发货数量 %@",  [noChangeOutQuantityArray objectAtIndex:indexPath.row]];
        cell.Amount.textColor=luseColor;
        cell.Amount.font=[UIFont systemFontOfSize:12];

    }
    if (itemsArray.count !=indexPath.row+1) {
        UILabel *lineLabel11=[[UILabel alloc] initWithFrame:CGRectMake(10, 137.5-0.45, ScreenWidth-20, 0.45)];
        lineLabel11.backgroundColor=qianhuiColor;
        [cell.contentView addSubview:lineLabel11];
        
    }
    
    if (itemsArray.count ==indexPath.row+1) {
        UILabel *lineLabel11=[[UILabel alloc] initWithFrame:CGRectMake(0, 137.5-0.45, ScreenWidth, 0.45)];
        lineLabel11.backgroundColor=qianhuiColor;
        [cell.contentView addSubview:lineLabel11];
        
    }
    
    return cell;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 137.5;
}

#pragma mark - Table view delegate
-(void)countBtn:(UIButton *)sender{
    

    UIButton* button=(UIButton * )sender;
    huiyuanDetailsCell* cell=(huiyuanDetailsCell* )button.superview.superview;
    UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"数量"
                               
                                                        message:@"请输入修改的数量"
                               
                                                       delegate:self
                               
                                              cancelButtonTitle:@"取消"
                               
                                              otherButtonTitles:@"确定",nil];
    alertView.tag=sender.tag;
    [alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
    UITextField * text1 = [alertView textFieldAtIndex:0];
    text1.text=cell.time.text;
    tagStr=[NSString stringWithFormat:@"%ld",  sender.tag];
    shuliang=cell.how;
    text1.contentVerticalAlignment= UIControlContentVerticalAlignmentCenter;
    text1.keyboardType = UIKeyboardTypeNumberPad;
    [alertView show];
}


-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==10000) {
        if (buttonIndex == 1)
        {
          

            [dealPriceArray removeObjectAtIndex:tagStr.intValue];//从指定索引移除
            [selectimageArray removeObjectAtIndex:tagStr.intValue];//从指定索引移除
            [itemIdArray removeObjectAtIndex:tagStr.intValue];//从指定索引移除
            [itemNameArray removeObjectAtIndex:tagStr.intValue];//从指定索引移除
            [outQuantityArray removeObjectAtIndex:tagStr.intValue];//从指定索引移除
            [noChangeOutQuantityArray removeObjectAtIndex:tagStr.intValue];//从指定索引移除
            [quantity removeObjectAtIndex:tagStr.intValue];//从指定索引移除
            [itemCodeArray removeObjectAtIndex:tagStr.intValue];//从指定索引移除
            [specNameArray removeObjectAtIndex:tagStr.intValue];//从指定索引移除
            [specIdArray removeObjectAtIndex:tagStr.intValue];//从指定索引移除
            [table reloadData];
            [self priceStr];
            
        }else{
        
        }
        
    }else{
    if (buttonIndex == 1)
        {
            UITextField * text1 = [alertView textFieldAtIndex:0];
            text1.contentVerticalAlignment= UIControlContentVerticalAlignmentCenter;
            text1.keyboardType = UIKeyboardTypeNumberPad;
            if (text1.text.intValue==0) {

                UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"是否确定删除该商品"
                                           
                                                                    message:@""
                                           
                                                                   delegate:self
                                           
                                                          cancelButtonTitle:@"取消"
                                           
                                                          otherButtonTitles:@"确定",nil];
                alertView.tag=10000;
                [alertView show];
            }else{
                UIAlertView* button=(UIAlertView * )alertView;
                huiyuanDetailsCell* cell=(huiyuanDetailsCell* )button.superview.superview.superview;
                
                NSLog(@"----%@",text1.text);
                if ([self.titleName isEqualToString:@"确认入库"]) {
                    
//                    if (shuliang.intValue>=text1.text.intValue) {
                    
                        NSMutableArray *mutableArray = [NSMutableArray arrayWithArray:outQuantityArray];
                        [mutableArray replaceObjectAtIndex:alertView.tag withObject:[NSString stringWithFormat:@"%@", text1.text]];
                        outQuantityArray = mutableArray;//now the array has been "updated"
                        
                        cell.time.text=[NSString stringWithFormat:@"%@", text1.text];
                        [self priceStr];
//                    }else{
//                        [self showAlertMessage:@"不能超过发货数量！"];
//                    }
                    }else{
                    NSMutableArray *mutableArray = [NSMutableArray arrayWithArray:outQuantityArray];
                    [mutableArray replaceObjectAtIndex:alertView.tag withObject:[NSString stringWithFormat:@"%@", text1.text]];
                    outQuantityArray = mutableArray;//now the array has been "updated"
                    cell.time.text=[NSString stringWithFormat:@"%@", text1.text];
                    [self priceStr];
                }
                
                [table reloadData];
            }
        }
    
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
-(void)jia:(UIButton*)sender{
    
    UIButton* button=(UIButton * )sender;
    huiyuanDetailsCell* cell=(huiyuanDetailsCell* )button.superview.superview;
    if ([self.titleName isEqualToString:@"确认入库"]) {
        
//        if (cell.how.intValue>cell.time.text.intValue) {
            int d;
            d= cell.time.text.intValue;
            d++;
            NSLog(@"---------%ld",sender.tag);
            NSMutableArray *mutableArray = [NSMutableArray arrayWithArray:outQuantityArray];
            [mutableArray replaceObjectAtIndex:sender.tag withObject:[NSString stringWithFormat:@"%d", d]];
            outQuantityArray = mutableArray;//now the array has been "updated"
            
            cell.time.text=[NSString stringWithFormat:@"%d", d];
            [self priceStr];
//        }else{
//            [self showAlertMessage:@"不能超过发货数量！"];
//        }

    }else{
        int d;
        d= cell.time.text.intValue;
        d++;
        NSLog(@"---------%ld",sender.tag);
        NSMutableArray *mutableArray = [NSMutableArray arrayWithArray:outQuantityArray];
        [mutableArray replaceObjectAtIndex:sender.tag withObject:[NSString stringWithFormat:@"%d", d]];
        outQuantityArray = mutableArray;//now the array has been "updated"
        cell.time.text=[NSString stringWithFormat:@"%d", d];
        [self priceStr];
    }
}
-(void)jian:(UIButton*)sender{
    UIButton* button=(UIButton * )sender;
    huiyuanDetailsCell* cell=(huiyuanDetailsCell* )button.superview.superview;
    if ([cell.time.text isEqualToString:@"1"]) {
        UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"是否确定删除该商品"
                                   
                                                            message:@""
                                   
                                                           delegate:self
                                   
                                                  cancelButtonTitle:@"取消"
                                   
                                                  otherButtonTitles:@"确定",nil];
        alertView.tag=10000;
        
        tagStr=[NSString stringWithFormat:@"%ld",sender.tag];
        [alertView show];

    }else{
        
        int d;
        d= cell.time.text.intValue;
        d--;
        NSLog(@"---------%ld",sender.tag);
        NSMutableArray *mutableArray = [NSMutableArray arrayWithArray:outQuantityArray];
        [mutableArray replaceObjectAtIndex:sender.tag withObject:[NSString stringWithFormat:@"%d", d]];
        outQuantityArray = mutableArray;//now the array has been "updated"
        cell.time.text=[NSString stringWithFormat:@"%d", d];
        [self priceStr];
    }
}
-(void)priceStr{
    NSMutableArray*  sum=[[NSMutableArray alloc]init];
    for (int i=0; i<outQuantityArray.count; i++) {
        NSString *typeCode1 = [outQuantityArray objectAtIndex:i];
        NSString *isSingle1 = [dealPriceArray objectAtIndex:i];
        [sum addObject:[NSString stringWithFormat:@"%.2f",  [typeCode1 doubleValue]*[isSingle1 doubleValue]]];
    }
    
    NSNumber *sum111 = [sum valueForKeyPath:@"@sum.floatValue"];
    NSString*GoldPrice=[NSString stringWithFormat:@"%@",sum111];
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setPositiveFormat:@"###,##0.00;"];
    NSString *formattedNumberString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:GoldPrice.floatValue]];
    [totalQuantity setTitle:[NSString stringWithFormat:@"   合计 ¥ %@",formattedNumberString] forState:UIControlStateNormal];
    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"   合计 ¥ %@",formattedNumberString]];
    
    [AttributedStr addAttribute:NSFontAttributeName
     
                          value:[UIFont systemFontOfSize:13]
     
                          range:NSMakeRange(0, 7)];
    
    [AttributedStr addAttribute:NSForegroundColorAttributeName
     
                          value:[UIColor whiteColor]
     
                          range:NSMakeRange(0, 7)];
    
    
    totalQuantity.titleLabel.attributedText = AttributedStr;
    NSNumber *sum11 = [outQuantityArray valueForKeyPath:@"@sum.floatValue"];
    [quantityBtn setTitle:[NSString stringWithFormat:@"   共 %@ 件商品",sum11] forState:UIControlStateNormal];
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    table .frame =CGRectMake(0, -250, ScreenWidth, ScreenHeight);
    [UIView commitAnimations];
    return self;
}
//回收键盘
-(void)OnTapDone{
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    table .frame =CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    [UIView commitAnimations];
    [tv resignFirstResponder];
}

- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}
- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}
-(void)textViewDidChange:(UITextView *)textView
{
    
    if (textView.text.length == 0) {
        [imagess removeFromSuperview];
        
        imagess=[[UIImageView alloc]initWithFrame:CGRectMake(15, 5, 51/3, 51/3)];
        imagess.image=[UIImage imageNamed:@"_0000_xingxifankui.png"];
        [tv addSubview:imagess];
        label.text = @"备注";
        
    }else{
        [imagess removeFromSuperview];
        label.text = @"";
    }
}
@end
